<template>
    <view class="page">
        <view class="backbanner">
            
        </view>
		<view class="addressform">
			<!-- <view class="flexRowC lineInfo">
			    <view  class="" style="flex:1 ">
			        <text >姓 &nbsp;&nbsp;&nbsp;&nbsp; 名 :</text>
			    </view>
			    <view class="" style="flex:3">
			        <u-input v-model="form_data.address_username" placeholder="请输入联系人信息"></u-input>
			    </view>
			</view> -->
			<view class="flexRowC lineInfo">
			    <view class="" style="flex:1">
			        联系电话:
			    </view>
			    <view class="" style="flex:3">
			        <u-input type="number" v-model="form_data.address_tel" @blur='textpho' placeholder="请输入手机号码"></u-input>
			    </view>
			</view>
			<view class="flexRowC lineInfo">
			    <view class="" style="flex:1">
			        所在地区:
			    </view>
			    <view class="" style="flex:3">
			        <u-input @click="show_region = !show_region" v-model="area_detail" placeholder="请选择地区" :disabled="true"></u-input>
			    </view>
			</view>
			<view class="flexRowC lineInfo">
			    <view class="" style="flex:1">
			        详细地址:
			    </view>
			    <view class="" style="flex:3">
			        <u-input placeholder="请输入详细地址" v-model="form_data.address_detail"></u-input>
			    </view>
			</view>
			<!-- <view class="flexRowC lineInfo icon-space-col">
			    <u-checkbox-group>
			    	<u-checkbox active-color="#EA6752" v-model="form_data.is_default" shape="circle">设为默认地址</u-checkbox>
			    </u-checkbox-group>
			</view> -->
			<button type="default" class="savebtn" @click="saveAddress">保存地址</button>
		</view>
		
        
        
        <view class="">
            <u-picker mode="region" v-model="show_region" :params="params" @confirm="selectRegion"></u-picker>
        </view>
    </view>
    
</template>

<script>
    export default {
        components: {
            
        },
        data() {
            return {
                address_id: 0,
                form_data: {
                    address_username: '',
                    address_tel: '',
                    province: '',
                    city: '',
                    area: '',
                    address_detail: '',
                    id: 0,
                    is_default: false,
                },
                area_detail: '',
                show_region: false,
                params : {
                	province: true,
                	city: true,
                	area: true
                }

            }
        },
        onLoad(options) {
            
            this.address_id = options.id || 0;
            // console.log(address)
            if(this.address_id == 0) {
              return false
            }else{
              this.getAddressInfo()
            }
            
        },
        methods: {
            selectRegion(params) {
                console.log(params)
                this.form_data.province = params.province.label;
                this.form_data.city = params.city.label;
                this.form_data.area = params.area.label;
                this.area_detail = this.form_data.province+this.form_data.city+this.form_data.area;
            },
            textpho(){
                let reg=/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.form_data.address_tel)
                if(!reg){
                    uni.showToast({
                        title:'手机号码有误',
                        icon:'none'
                    })
                }
            },
            saveAddress() {
                let parmas={
                    pTel:this.form_data.address_tel,
                    pPlace:this.form_data.province+this.form_data.city+this.form_data.area+this.form_data.address_detail
                }
                let pPlace=this.form_data.province+this.form_data.city+this.form_data.area+this.form_data.address_detail
                if(this.address_id > 0) {
                    
                    this.$http.get('goods_user_position/add',parmas).then(res => {
                        uni.showToast({
                            icon:'none',
                            title: '更新成功',
                        })
                        // setTimeout(function(){
                        //     uni.navigateBack()
                        // }, 500)
                    })
                }else{
                    this.$http.get('goods_user_position/add?pTel='+this.form_data.address_tel+'&pPlace='+pPlace).then(res => {
                        uni.showToast({
                            icon:'none',
                            title: '添加成功',
                        })
                        setTimeout(function(){
                            uni.navigateBack()
                        }, 500)
                    })
                }
                
            },
            getAddressInfo() {
                this.$http.get('/address/'+this.address_id).then(res => {
                    this.form_data = res.data;
                    this.area_detail = this.form_data.province+this.form_data.city+this.form_data.area;
                })
            }
        },
    }
</script>

<style lang="scss" scoped>
    .backbanner{
		width: 750rpx;
		height: 158rpx;
		background-image: linear-gradient(to bottom,#EA6652 , #F1874E);
		
	}
	.addressform{
		
		width: 714rpx;
		height: 1162rpx;
		background-color: #fff;
		margin-top: -68rpx;
		margin-left: 18rpx;
		border-radius: 10rpx;
		padding: 0 20rpx;
	}
	.savebtn{
		margin-top: 173rpx;
			width: 610rpx;
			height: 90rpx;
			line-height: 90rpx;
			color: #fff;
			font-size: 30rpx;
			background-image: linear-gradient(to right, #EA6652 , #F1874E);
		}
	.lineInfo{
		width: 100%;
		height: 112rpx;
		border-bottom: 1rpx solid #F4F4F4;
		padding: 0 40rpx;
	}
</style>

